//
//  QualificationController.m
//  APJ Resource
//
//  Created by Richard Zhao on 2/18/14.
//  Copyright (c) 2014 APJ. All rights reserved.
//

#import "QualificationController.h"
#import "QualificationCell.h"
#import "Qualification.h"
#import "HRStore.h"

@implementation QualificationController
{
    NSArray *datas;
}

- (id)initWithStaffId:(int)staffId
{
    self = [super init];
    
    if (self) {
        datas = [[HRStore sharedStore] qualificationOfStaff:staffId];
    }
    
    return self;
}

- (id)init
{
    [NSException raise:@"Qualifcation init error" format:@"please call initWithStaffId."];
    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QualificationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"qual"];
    if(cell==nil){
        cell = [[[NSBundle mainBundle]loadNibNamed:@"QualificationCell" owner:self options:nil] lastObject];
    }
    
    Qualification *item = [datas objectAtIndex:indexPath.row];
    cell.nameLabel.text = item.qualifcationName;
    cell.institutionLabel.text = item.issueInstitution;
    cell.commentsLabel.text = item.comments;
    
    NSDate *fromDate = [NSDate dateWithTimeIntervalSince1970:item.effStartDate];
    NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:item.effEndDate];
    NSDateFormatter *formate = [[NSDateFormatter alloc] init];
    formate.dateFormat = @"YYYY-mm-dd";
    cell.periodLabel.text = [NSString stringWithFormat:@"%@ --- %@", [formate stringFromDate:fromDate], [formate stringFromDate:endDate]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 142;
}
@end
